package router

import (
	"gin_zebra/app/ctrl"
	"gin_zebra/app/middleware"
	"gin_zebra/public"
	"gitee.com/xiedongji/go-core/conf"
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
	"log"
)

/* =================================================================
	路由 主入口 在此处！！！！
 * =================================================================*/
func InitRouterHandler(middlewares ...gin.HandlerFunc) *gin.Engine {

	//构造路由器
	router := gin.Default()
	router.Use(middlewares...)
	router.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})

	//使用Redis进行存储
	store, err := sessions.NewRedisStore(10, "tcp",
		conf.GetStringConf("base.session.address"),
		conf.GetStringConf("base.session.auth"), []byte("secret"))
	if err != nil {
		log.Fatalf("sessions.NewRedisStore err:%v", err)
	}
	/* =================================================================
		                           路由分组
	 * =================================================================*/

	//=====================userUnGroup====================
	userUnAuthGroup := router.Group("userun")
	userUnAuthGroup.Use(
		//middleware.RecoveryMiddleware(),
		sessions.Sessions(public.SessionName, store),
		middleware.TranslationMiddleware(),
	)
	{
		ctrl.UserUnAuthRegister(userUnAuthGroup)
	}
	//=====================userGroup====================
	userGroup := router.Group("user")
	userGroup.Use(
		//middleware.RecoveryMiddleware(),
		sessions.Sessions(public.SessionName, store),
		middleware.SessionAuthMiddleware(),
		middleware.TranslationMiddleware(),
	)
	{
		ctrl.UserRegister(userGroup)
	}

	return router
}
